import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import('components/common/Login')
const Home = () => import('view/home/Home')
const Welcome = () => import('view/home/childComps/Welcome')
const Users = () => import('view/home/childComps/users/Users')
const Rights = () => import('view/home/childComps/power/Rights')
const Roles = () => import('view/home/childComps/power/Roles')
const Cate = () => import('view/home/childComps/goods/Cate')



Vue.use(VueRouter)

const routes = [
    {
        path:'/',
        redirect:'/login'
    },
    {
        path:'/login',
        component:Login
    },
    {
        path:'/home',
        component:Home,
        children:[
            {
                path:'',
                redirect:'/welcome'
            },
            {
                path:'/welcome',
                component:Welcome
            },
            {
                path:'/users',
                component:Users
            },
            {
                path:'/rights',
                component:Rights
            },
            {
                path:'/roles',
                component:Roles
            },
            {
                path:'/categories',
                component:Cate
            }
        ]
    }
]
const router = new VueRouter({
    routes,
    mode:'history'
})

router.beforeEach((to,from,next) => {
    if(to.path == '/login'){
        return next()
    }

    const tokenStr = window.sessionStorage.getItem('token')
    if(!tokenStr){
        return next('/login')
    }
    next()
})

export default router